package com.dupengcheng.gateway.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.dupengcheng.gateway.constant.RedisConstants;
import com.dupengcheng.gateway.entity.AuthPermission;
import com.dupengcheng.gateway.redis.RedisUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限验证接口扩展 
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private RedisUtil redisUtil;
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的权限列表

        List<String> permissList=new LinkedList<>();
        permissList.add("user:add");
        permissList.add("user:query");

        String permissionKey = redisUtil.buildKey(RedisConstants.AUTH_PERMISSION_PREFIX, loginId.toString());
        String key     = redisUtil.get(permissionKey);
        List<String> permission=new LinkedList<>();
        if (StringUtils.isEmpty(key)){
            return Collections.emptyList();
        }
        List<AuthPermission> authPermissions = new Gson().fromJson(key, new TypeToken<List<AuthPermission>>() {
        }.getType());
        permission=authPermissions.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return permission;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的角色列表
        List<String> roleList=new LinkedList<>();
        roleList.add("custom");//游客
        return roleList;
    }

}
